// Copyright (c) 2009 Hypirinha contributers
// This program is made available under the terms of the MIT License.
package org.hypirinha.html.codegen;

import static org.hypirinha.html.codegen.CodegenUtil.capitalise;

import java.util.Collection;
import java.util.SortedSet;
import java.util.TreeSet;

public class ElementFactory {

    private SortedSet<String> elements = new TreeSet<String>();

    public ElementFactory(Collection<String> elements) {
        this.elements.addAll(elements);
    }

    public GeneratedClass createImplementation() {
        GeneratedClass generatedClass = new GeneratedClass("org.hypirinha.html", "ElementFactory");
        generatedClass.addImport("org.hypirinha.html.elements.*");
        if (elements.contains("object")) {
            generatedClass.addImport("org.hypirinha.html.elements.Object");
        }
        generatedClass.addImport("org.hypirinha.html.elements.implementations.*");
        for (String element : elements) {
            ConcreteMethod method = new ConcreteMethod(element, capitalise(element), capitalise(element) + ".Attribute... attributes",
                    "return new " + capitalise(element) + "Element(attributes);");
            method.getModifiers().add("static");
            generatedClass.getMembers().add(method);
        }
        ConcreteMethod textMethod = new ConcreteMethod("text", "Text", "String content", "return new Text(content);");
        textMethod.getModifiers().add("static");
        generatedClass.getMembers().add(textMethod);
        return generatedClass;
    }
}
